package com.qinggeng.project.system.service.impl;
import com.qinggeng.common.enums.MemberEnum;
import com.qinggeng.project.system.domain.GoodsVip;
import com.qinggeng.project.system.domain.Member;
import com.qinggeng.project.system.mapper.GoodsVipMapper;
import com.qinggeng.project.system.service.GoodsVipService;
import com.qinggeng.project.system.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class GoodsVipServiceImpl implements GoodsVipService {
    @Autowired
    private GoodsVipMapper goodsVipMapper;
    @Autowired
    private MemberService memberService;

    @Override
    public List<GoodsVip> listGoodsVip(Long userId) {
        Example example=new Example(GoodsVip.class);
        example.createCriteria()
                .andEqualTo("status","10")
                .andEqualTo("delFalg",10);
       List<GoodsVip> list= goodsVipMapper.selectByExample(example);
       if (list==null){
           list=new ArrayList<>();
       }
        Iterator<GoodsVip> iterator= list.iterator();
        while (iterator.hasNext()){
            GoodsVip goodsVip=iterator.next();
            if ("普通会员".equals(goodsVip.getSupplierDesc())){
                Integer type=memberService.isVip(userId);
                if (type.equals(MemberEnum.MEMBER.getKey())){
                    iterator.remove();
                }
            }
            if ("超级会员".equals(goodsVip.getSupplierDesc())){
                Integer type=memberService.isSVip(userId);
                if (type.equals(MemberEnum.MEMBER.getKey())){
                    iterator.remove();
                }
            }
        }
        return list;
    }

    @Override
    public GoodsVip goodsVip(Long id) {
        Example example = new Example(GoodsVip.class);
        example.createCriteria()
                .andEqualTo("goodsId", id)
                .andEqualTo("status", "10")
                .andEqualTo("delFalg", 10);
        return goodsVipMapper.selectOneByExample(example);
    }
}
